package com.uzai.console.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.uzai.console.common.web.interceptor.SysInterceptor;
import com.uzai.console.common.web.interceptor.TraceIdInterceptor;
import com.uzai.console.common.web.interceptor.UserInfoFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置MVC拦截
 * @Auther: liuqi
 * @Date: 2019/12/12 19:48
 * @Description:
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // traceId拦截器
        registry.addInterceptor(traceIdInterceptor()).addPathPatterns("/**").order(0);

        registry.addInterceptor(sysInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/csrf/", "/wx/access",
                        "/loginByCode", "/loginByPwd", "/getCode", "/register", "/tb/callback", "/tb/callback/new", "/tb/oauth", "/tb/oauth/new", "/tb/callback/api", "/pdd/callback", "/pdd/oauth", "/vph/callback", "/vph/oauth",
                        "/wechat/minipro/callback", "/sysMenu/uploadSysMenu", "/loginByChildAccountPwd", "/loginByUzaiAccountPwd", "/alipay/notify", "/alipay/return", "/alipay/return",
                        "/alipayDetail/findAlipayUrl", "/alipayDetail/alipayTradeQuery", "/alipay/mchinvitesign/url/findById", "/cdkeys/oldCardExchange", "/xxlJob/deviceOffLineAlarmMsg", "/xxlJob/deviceExpiredAlarmMsg", "/xxlJob/**",
                        "/xxlJob/downLoadHdkGoodsSync", "/xxlJob/autoSendPyq", "/wxpub/addWxpubAuthFree", "/pyq/followSend", "/alipay/cdkey/findAlipayDetailList", "/alipay/cdkey/queryTradeNo",
                        "/pyq/followSendContent", "/pyq/followSendComment", "/alipay/mchappauth/result", "/alipay/mchappauth/query", "/xxlJob/autoDownloadTbPunishOrder", "/groupSendJob/repairWxContactGroupSendJob",
                        "/sendAlarmMsg/tbOrdersDownLimit", "/romoteApi/**", "/romoteApi/uploadTbPunishOrderByUzaiToolBox", "/romoteApi/wechatuser/sendMsg", "/romoteApi/wechatuser/updateInfo", "/romoteApi/wechatuser/updateDevice",
                        "/romoteApi/cashier/getAuthpayContent", "/fd/followSend", "/fd/followQfSend", "/reportPanel/fansValue/findById", "/romoteApi/reportBanel/fansValueReport",
                        "/godApi/**", "/dataPanel/findById", "/dy/callback", "/dy/oauth", "/ks/callback","/captcha/getCaptcha","/mini/fastregister/callback","/mini/auth/callback","/mini/checkticket/callback",
                        "/miniCode/template/findByList",
                        "/test/**","/dingDingCallBack/**"
                );
        super.addInterceptors(registry);
    }

    @Bean
    public TraceIdInterceptor traceIdInterceptor() {
        return new TraceIdInterceptor();
    }

    @Bean
    public SysInterceptor sysInterceptor(){
        return new SysInterceptor();
    }

    @Bean
    public FilterRegistrationBean servletRegistrationBean() {
        FilterRegistrationBean<UserInfoFilter> bean = new FilterRegistrationBean<>();
        UserInfoFilter userInfoFilter = new UserInfoFilter();
        bean.setFilter(userInfoFilter);
        bean.addUrlPatterns("/*");
        return bean;
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);

        objectMapper.registerModule(simpleModule);

        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //放到第一个
        converters.add(0, jackson2HttpMessageConverter);
        super.configureMessageConverters(converters);
    }
}
